import navBottom from '../common/nav-bottom.vue'
import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';
import './index.scss';
// import { FlexboxItem, XProgress, CheckIcon, Scroller, LoadMore } from 'vux'
import XHeader from 'vux/src/components/x-header/index.vue'
import Flexbox from 'vux/src/components/flexbox/flexbox.vue'
import FlexboxItem from 'vux/src/components/flexbox/flexbox-item.vue'
import XProgress from 'vux/src/components/x-progress/index.vue'
import CheckIcon from 'vux/src/components/check-icon/index.vue'
import Scroller from 'vux/src/components/scroller/index.vue'
import LoadMore from 'vux/src/components/load-more/index.vue'

export default {
  name: 'batch',
  components: {navBottom, XHeader, Flexbox, FlexboxItem, XProgress, CheckIcon, Scroller, LoadMore},
  data () {
    return {
      quanbu: true,
      zhukun: false,
      zhuxue: false,
      zhuyi: false,
      qita: false,
      pageNo: 1,
      quanbuPageNo: 1,
      zhukunPageNo: 1,
      zhuxuePageNo: 1,
      zhuyiPageNo: 1,
      qitaPageNo: 1,
      pageSize: 5,
      event_type: '',
      eventList: [],
      quanbuList: [],
      zhukunList: [],
      zhuxueList: [],
      zhuyiList: [],
      qitaList: [],
      ids: [],
      show_loading: false,
      tipText: '',
      use_pullup: true,
      pullup_con: {
        content: ' ',
        pullUpHeight: 60,
        height: 40,
        autoRefresh: false,
        downContent: ' ',
        upContent: ' ',
        loadingContent: ' ',
        clsPrefix: 'xs-plugin-pullup-'
      },
      // 全选按钮
      allChecked: false,
      // 单个捐助金额
      oneDonation: 10,
      selectedItems: 0,
      allDonation: 0,
      // 同意按钮
      agree: true
    }
  },
  mounted () {
    console.log(this.piliangjuan)
    this.goEventList(this.quanbuPageNo, this.event_type, this.quanbuList)
    if (this.$emit('ifOpenid')) {
      this.$emit('ifOpenid', this.$route.path)
    }
  },
  watch: {
    allChecked: function () {
      if (this.allChecked == true) {
        for (let a = 0; a < this.eventList.length; a++) {
          this.eventList[a].ifChecked = true
        }
        this.allDonation = (Number(this.oneDonation) * Number(this.selectedItems)).toFixed(2)
        // this.allDonation = this.oneDonation * this.selectedItems
      } else if (this.allChecked == false) {
        for (let a = 0; a < this.eventList.length; a++) {
          this.eventList[a].ifChecked = false
        }
        this.selectedItems = 0
        this.allDonation = (Number(this.oneDonation) * Number(this.selectedItems)).toFixed(2)
      }
    },
    selectedItems: function () {
      this.allDonation = (Number(this.oneDonation) * Number(this.selectedItems)).toFixed(2)
    },
    oneDonation: function () {
      this.allDonation = (Number(this.oneDonation) * Number(this.selectedItems)).toFixed(2)
      if (this.oneDonation == '') {

      } else {
        let zz = /^\d*\.{0,1}\d{0,2}$/
        if (!zz.test(this.oneDonation)) {
          this.oneDonation = ''
          alert('请输入整数或者小数\n（提示：小数最多为两位小数）')
          return
        }
      }
    },
    event_type: function () {
      for (let a = 0; a < this.eventList.length; a++) {
        this.eventList[a].ifChecked = false
        this.allChecked = false
      }
    },
    eventList: {
      handler: function () {
        let num = 0;
        for (let a = 0; a < this.eventList.length; a++) {
          // this.eventList[a].ifChecked = false
          // this.allChecked = false
          if (this.eventList[a].ifChecked == true) {
            num++
          } else {

          }
        }
        this.selectedItems = num
      },
      deep: true
    }
  },
  methods: {
  	goProjectDetails (id) {
  		this.$router.push({ path: '/Project',query:{event_id:id}})
  	},
    wantDonate () {
      // 我要捐助按钮绑定事件
      let jin = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
      if (!jin.test(this.oneDonation)) {
        alert('请输入正确的金额！\n（提示：小数最多为两位小数，不能以.开头,开头为0后必须跟.）')
      } else if (this.oneDonation == 0) {
        alert('请输入单笔捐款金额！')
      } else if (this.agree == false) {
        alert('请同意亲青筹捐款协议！')
      } else {
        let _this = this
        _this.ids = []
        for (let i = 0; i < _this.eventList.length; i++) {
          if (_this.eventList[i].ifChecked == true) {
            _this.ids.push(_this.eventList[i].id)
          }
        }
        if (this.ids.length == 0) {
          alert('请选择捐款的对象')
          return
        }
        console.log(this.ids)
        this.$router.push({
          path: '/payment',
          query: {
            witchOne: 'piliang',
            number: this.ids.length,
            ids: this.ids,
            money: this.oneDonation
          }
        })
      }
    },
    // onScrollBottom () {
    //   this.$nextTick(() => {
    //     this.$refs.scroller.donePullup()
    //     this.$refs.scroller.reset()
    //   })
    //   if (this.event_type == '') {
    //     this.goEventList(this.quanbuPageNo, this.event_type, this.quanbuList)
    //   } else if (this.event_type == 'pool') {
    //     this.goEventList(this.zhukunPageNo, this.event_type, this.zhukunList)
    //   } else if (this.event_type == 'study') {
    //     this.goEventList(this.zhuxuePageNo, this.event_type, this.zhuxueList)
    //   } else if (this.event_type == 'medicine') {
    //     this.goEventList(this.zhuyiPageNo, this.event_type, this.zhuyiList)
    //   } else if (this.event_type == 'other') {
    //     this.goEventList(this.qitaPageNo, this.event_type, this.qitaList)
    //   }
    // },
    changeNo () {
      for (let a = 0; a < this.eventList.length; a++) {
        this.eventList[a].ifChecked = false
      }
    },
    tabChange (e) {
      if (e == 'quanbu') {
        this.quanbu = true
        this.zhukun = false
        this.zhuxue = false
        this.zhuyi = false
        this.qita = false
        // 全部
        this.event_type = ''
        // this.changeNo()
        if (this.quanbuPageNo == 1) {
          this.goEventList(this.quanbuPageNo, this.event_type, this.quanbuList)
        } else {
          this.eventList = this.quanbuList
        }
      } else if (e == 'zhukun') {
        this.quanbu = false
        this.zhukun = true
        this.zhuxue = false
        this.zhuyi = false
        this.qita = false
        // 助困
        this.event_type = 'pool'
        if (this.zhukunPageNo == 1) {
          this.goEventList(this.zhukunPageNo, this.event_type, this.zhukunList)
        } else {
          this.eventList = this.zhukunList
        }
      } else if (e == 'zhuxue') {
        this.quanbu = false
        this.zhukun = false
        this.zhuxue = true
        this.zhuyi = false
        this.qita = false
        // 助学
        this.event_type = 'study'
        if (this.zhuxuePageNo == 1) {
          this.goEventList(this.zhuxuePageNo, this.event_type, this.zhuxueList)
        } else {
          this.eventList = this.zhuxueList
        }
      } else if (e == 'zhuyi') {
        this.quanbu = false
        this.zhukun = false
        this.zhuxue = false
        this.zhuyi = true
        this.qita = false
        // 助医
        this.event_type = 'medicine'
        if (this.zhuyiPageNo == 1) {
          this.goEventList(this.zhuyiPageNo, this.event_type, this.zhuyiList)
        } else {
          this.eventList = this.zhuyiList
        }
      } else if (e == 'qita') {
        this.quanbu = false
        this.zhukun = false
        this.zhuxue = false
        this.zhuyi = false
        this.qita = true
        // 其他
        this.event_type = 'other'
        if (this.qitaPageNo == 1) {
          this.goEventList(this.qitaPageNo, this.event_type, this.qitaList)
        } else {
          this.eventList = this.qitaList
        }
      }
    },
    goEventList (Num, type, list) {
      if (Num != 1) {
        this.tipText = '正在加载'
        this.show_loading = true
      }
      http.post(CONSTANT.BATCH_LIST, {event_type: type}, (res) => {
           console.log(res)
        if (res.code == 200) {
          if (res.errmsg == '没有更多的项目') {
            this.tipText = '没有更多了'
            this.show_loading = false
            console.log('没了，别刷了！')
            return
          }
          let lists = res.list;
          for (let a = 0; a < lists.length; a++) {

            lists[a].ifChecked = false;
          }
          if (Num == 1) {
            if (type == '') {
              this.quanbuPageNo++;
              this.quanbuList = lists
              if (this.quanbu == true) {
                this.eventList = this.quanbuList
              }
            } else if (type == 'pool') {
              this.zhukunPageNo++;
              this.zhukunList = lists
              if (this.zhukun == true) {
                this.eventList = this.zhukunList
              }
            } else if (type == 'study') {
              this.zhuxuePageNo++;
              this.zhuxueList = lists
              if (this.zhuxue == true) {
                this.eventList = this.zhuxueList
              }
            } else if (type == 'medicine') {
              this.zhuyiPageNo++;
              this.zhuyiList = lists
              if (this.zhuyi == true) {
                this.eventList = this.zhuyiList
              }
            } else if (type == 'other') {
              this.qitaPageNo++;
              this.qitaList = lists
              if (this.qita == true) {
                this.eventList = this.qitaList
              }
            }
          } else {
            if (type == '') {
              this.quanbuPageNo++;
              this.quanbuList = this.quanbuList.concat(lists)
              if (this.quanbu == true) {
                this.eventList = this.quanbuList
              }
            } else if (type == 'pool') {
              this.zhukunPageNo++;
              this.zhukunList = this.zhukunList.concat(lists)
              if (this.zhukun == true) {
                this.eventList = this.zhukunList
              }
            } else if (type == 'study') {
              this.zhuxuePageNo++;
              this.zhuxueList = this.zhuxueList.concat(lists)
              if (this.zhuxue == true) {
                this.eventList = this.zhuxueList
              }
            } else if (type == 'medicine') {
              this.zhuyiPageNo++
              this.zhuyiList = this.zhuyiList.concat(lists)
              if (this.zhuyi == true) {
                this.eventList = this.zhuyiList
              }
            } else if (type == 'other') {
              this.qitaPageNo++;
              this.qitaList = this.qitaList.concat(lists)
              if (this.qita == true) {
                this.eventList = this.qitaList
              }
            }
          }
        } else {
          console.log('出错了')
        }
      })
    },
    isagree() {
      this.agree = !this.agree
    },
    Checked(e) {
      this.$forceUpdate()
    }
  }
}
